package com.easy.common.exception;

import com.easy.common.core.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理程序
 *
 * @author RuoYu
 * @date 2022/11/18
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 基础异常
     */
    @ExceptionHandler(BaseException.class)
    public R<String> baseException(BaseException e) {
        log.error(e.getMessage());
        return R.error(e.getMessage()+"1");
    }

    /**
     * 服务器内部异常
     */
    @ExceptionHandler(Exception.class)
    public R<String> severException(Exception e) {
        log.error(e.getMessage());
        return R.error("服务器内部异常"+"2");
    }

    /**
     * 业务异常
     */
    @ExceptionHandler(CustomException.class)
    public R businessException(CustomException e) {
        if (e.getCode() != null) {
            System.out.println("+=================1");
            return R.error(e.getCode());
        }
        System.out.println("+=================2");
        return R.error(e.getMessage());
    }

    /**
     * 工具类异常
     *
     * @param e e
     * @return {@link R}
     */
    @ExceptionHandler(UtilException.class)
    public R utilsException(UtilException e) {
        return R.error(e.getMessage()+"3");
    }
}